const util = require('../../utils/util')
const pinyin = require('../../utils/pinyin.js')
const app = getApp()
let temp = ['da啊象', '每个', 'px啊j', '发生的', '宅男', '二次元', '大12', '测试', '爱好l', '是', '阿三', '呐的', '猫', '狗', '12生', '吃12']
Page({
  data: {
    hidden: true,
    indexID: '',
    isDel: false,
    selectedNumber: 0,
    clubId: '',
    normalList: [],
    searchList: [],
    searchVal: '',
    list: '',
    usersLength: 0,
    colorArr: [],
  },
  onReady() {
    let that = this;
    wx.createSelectorQuery().select('.indexBar-box').boundingClientRect(function (res) {
      that.setData({
        boxTop: res.top
      })
    }).exec();
    wx.createSelectorQuery().select('.indexes').boundingClientRect(function (res) {
      that.setData({
        barTop: res.top
      })
    }).exec()
    wx.createSelectorQuery().select("#scroll").boundingClientRect((res) => {
      console.log('高度', res);
    }).exec();
  },
  // onPageScroll: function (e) {
  //   console.log(e.scrollTop) //这个就是滚动到的位置,可以用这个位置来写判断
  // },
  onLoad() {
    // 生成右侧26个字母和#
    let list = [];
    let colorArr = []
    for (let i = 0; i < 26; i++) {
      list[i] = String.fromCharCode(65 + i)
    }

    for (let i = 0; i < temp.length; i++) {
      colorArr.push(util.color())
    }

    list.push('#')
    this.setData({
      list,
      listCur: list[0],
      colorArr
    })



    this.init()
    this.SegSort()
    this.ml_isColor()
  },
  // 初始化数据
  init() {
    let tempList = []
    console.log('初始化数据temp', temp);
    temp.forEach((item, index) => {
      tempList.push({
        'uid': index + 1,
        'nickName': item,
      })
    });
    this.setData({
      normalList: tempList,
      usersLength: tempList.length
    })
  },
  // 随机颜色
  ml_isColor() {
    let that = this,
      typeList = '50',
      colorArr = that.data.colorArr,
      colorLen = colorArr.length,
      randomColorArr = []
    //判断执行
    do {
      let random = colorArr[Math.floor(Math.random() * colorLen)]
      randomColorArr.push(random)
      typeList--
    } while (typeList > 0)
    that.setData({
      randomColorArr: randomColorArr
    })
  },
  //获取文字信息
  getCur(e) {
    this.setData({
      hidden: false,
      listCur: this.data.list[e.target.id],
    })
  },

  setCur(e) {
    this.setData({
      hidden: true,
      listCur: this.data.listCur
    })
  },
  //滑动选择Item
  tMove(e) {
    let y = e.touches[0].clientY,
      offsettop = this.data.boxTop,
      that = this;
    //判断选择区域,只有在选择区才会生效
    if (y > offsettop) {
      let num = parseInt((y - offsettop) / 20);
      this.setData({
        listCur: that.data.list[num]
      })
    };
  },

  //触发全部开始选择
  tStart() {
    this.setData({
      hidden: false
    })
  },

  //触发结束选择
  tEnd() {
    this.setData({
      hidden: true,
      indexID: this.data.listCur
    })
  },
  SegSort() {
    let that = this;
    // 汉字
    let zharr = JSON.parse(JSON.stringify(that.data.normalList))
    // 拼音
    let pinyinarr = pinyin.pySegSort(that.data.normalList)
    for (let key in pinyinarr) {
      for (let k in zharr) {
        //下面一层循环是为了找到通过pySegSort函数处理后生成的数组的各个数据
        for (let z in pinyinarr[key].data) {
          if (pinyinarr[key].data[z].uid == zharr[k].uid) {
            pinyinarr[key].data[z] = zharr[k]
            pinyinarr.forEach(item => {
              item.data.forEach(item1 => {
                item1.isSelected = false
              })
            })
            that.setData({
              normalList: pinyinarr
            })
          }
        }
      }
    }
  },
  searchVal(e) {
    this.setData({
      searchVal: e.detail.value
    })
  },
  // 添加用户
  addUsers() {
    if (this.data.searchVal !== '') {
      temp.push(
        this.data.searchVal
      )
      wx.showToast({
        title: '添加成功',
        icon: 'none',
        image: '',
        duration: 1500,
        mask: false,
        success: (result) => {},
        fail: () => {},
        complete: () => {}
      });
      this.init()
      this.SegSort()
      this.setData({
        searchVal: ''
      })
    }
  },
});